<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>添加会员</span>
      </div>
      <el-form :model="member" :rules="rules" ref="memberForm" label-width="120px">
        <el-form-item label="账户名" prop="username">
          <el-input v-model="member.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="member.password" type="password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('memberForm')">提交</el-button>
          <el-button @click="resetForm('memberForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import request from '../../utils/request';

export default {
  name: 'AddUser',
  data() {
    return {
      member: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '请输入账户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          request.post('/user/add', this.member)
              .then(response => {
                this.$message.success('会员添加成功');
                this.resetForm(formName);
              })
              .catch(error => {
                console.error('Error adding member:', error);
                this.$message.error('会员添加失败');
              });
        } else {
          console.log('表单验证失败');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style scoped>
.box-card {
  margin: 20px;
}
</style>